package com.coffee.admin.controller.supplier;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.supplier.query.SupplierRoleQuery;
import com.coffee.admin.api.supplier.req.SupplierRoleListReq;
import com.coffee.admin.api.supplier.req.SupplierRoleReq;
import com.coffee.admin.api.supplier.result.SupplierPermissionResult;
import com.coffee.admin.api.supplier.result.SupplierRoleResult;
import com.coffee.admin.api.supplier.service.SupplierPermissionService;
import com.coffee.admin.api.supplier.service.SupplierRoleService;
import com.coffee.admin.controller.BaseController;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.SupplierLoginCheck;
import com.coffee.core.entity.LoginUser;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/supplier/role")
public class SupplierRoleController extends BaseController {

    @DubboReference(check = false)
    private SupplierRoleService supplierRoleService;

    @DubboReference(check = false)
    private SupplierPermissionService supplierPermissionService;

    @OperLog(operModul = "供应商-系统设置-权限管理", operDesc = "查询角色列表")
    @SupplierLoginCheck(alias = "SupplierRoleListAlias")
    @RequestMapping(value = "/findList")
    public Result<Page<SupplierRoleResult>> findList(HttpServletRequest request, @RequestBody SupplierRoleListReq params) {
        LoginUser supplier = getSupplier();
        Result<Page<SupplierRoleResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(params.getPageSize(), params.getPageIndex());
        SupplierRoleQuery supplierRoleQuery = new SupplierRoleQuery();
        supplierRoleQuery.setSupplierId(supplier.getSupplierId());
        result.setData(supplierRoleService.getPager(supplierRoleQuery, pagerInfo));
        return result;
    }

    @OperLog(operModul = "供应商-系统设置-权限管理", operDesc = "删除角色")
    @SupplierLoginCheck(alias = "SupplierRoleOperationAlias")
    @RequestMapping(value = "/delete")
    public Result<String> deleteRole(@RequestBody JSONObject json) {
        LoginUser supplier = getSupplier();
        Result<String> result = Result.success(null);
        Integer id = json.getInteger("id");
        supplierRoleService.delete(id, supplier.getSupplierId());
        return result;
    }

    @OperLog(operModul = "供应商-系统设置-权限管理", operDesc = "获取供应商所有权限")
    @SupplierLoginCheck(alias = "SupplierRoleListAlias")
    @RequestMapping(value = "/permission/getAll")
    public Result<List<SupplierPermissionResult>> getAllPermission(HttpServletRequest request) {
        Result<List<SupplierPermissionResult>> result = Result.success(null);
        result.setData(supplierPermissionService.getPermissionListByLoginMerchant());
        return result;
    }

    @OperLog(operModul = "供应商-系统设置-权限管理", operDesc = "保存角色")
    @SupplierLoginCheck(alias = "SupplierRoleOperationAlias")
    @RequestMapping(value = "/save")
    public Result<String> save(@RequestBody SupplierRoleReq roleReq) {
        Result<String> result = Result.success(null);
        try {
            roleReq.setSupplierId(getSupplier().getSupplierId());
            supplierRoleService.addOrUpdate(roleReq);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        return result;
    }

    @OperLog(operModul = "供应商-系统设置-权限管理", operDesc = "修改角色")
    @SupplierLoginCheck(alias = "SupplierRoleOperationAlias")
    @RequestMapping(value = "/update")
    public Result<String> update(@RequestBody SupplierRoleReq roleReq) {
        Result<String> result = Result.success(null);
        try {
            supplierRoleService.addOrUpdate(roleReq);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        return result;
    }

}
